package com.crm.controller.adminController;

import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.crm.dao.SourcesDAO;
import com.crm.dao.SupplierDAO;
import com.crm.entity.Business;
import com.crm.entity.Sources;
import com.crm.entity.Supplier;
import com.crm.entity.SupplierUser;


	@Controller
	@RequestMapping("/source")
public class SourceController {
		protected final Log logger = LogFactory.getLog(getClass());
		SourcesDAO sourcesDAO=new SourcesDAO();
		SupplierDAO supplierDAO=new SupplierDAO();
		Sources s=null;
		List<Sources> sourcesList=null;
		
		@RequestMapping("/open.do")
		public ModelAndView open(HttpSession session,ModelAndView model){
			Business bus=(Business)session.getAttribute("business");
			if(bus==null)return new ModelAndView("/jsp/index.jsp");
			model.setViewName("/jsp/Sources.jsp");
			model.addObject("submenu", "admin");
			model.addObject("menuName","admin");
			return model;
			
		}
		@RequestMapping("/SourceNameCheck.do")
		public @ResponseBody String busNameCheck(@RequestParam(value="name") String name,HttpSession session)throws IOException{
			
			Business bus=(Business)session.getAttribute("business");
			int busId=bus.getBusinessId();
			String b="false";
			
			try {
				b=sourcesDAO.sourceCheck(busId,name.trim());
			
			} catch (Exception e) {
				e.printStackTrace();
			}
			return b;
			
		}
		
		@RequestMapping(value="/save.do",method = RequestMethod.POST)
		public ModelAndView insert(HttpServletRequest req,HttpSession session, ModelAndView model) throws Exception{
			Business bus=(Business)session.getAttribute("business");
			if(bus==null)return new ModelAndView("/jsp/index.jsp");
			String name=req.getParameter("name");
			String email=req.getParameter("email");
			String phone=req.getParameter("phone");
			String address=req.getParameter("address");
			String website=req.getParameter("website");
			String fax=req.getParameter("fax");
			String country=req.getParameter("country");
			String cell=req.getParameter("cell");
			String assupplier=req.getParameter("assupplier");
			s=new Sources(bus,name,email,phone,address,website,fax,country,cell);
			logger.info("Sources Are"+s);
			sourcesDAO.insert(s);
			if(assupplier!=null){
				String rcount=req.getParameter("rowcount");
				logger.info("the row count is"+rcount);
				int rowcount=Integer.parseInt(rcount);
				
				Supplier su=new Supplier(bus,name,website,email,fax,phone,address,null,false);
				

					Set<SupplierUser> setSup = new HashSet<SupplierUser>();

					for (int i = 1; i <= rowcount; i++) {
						String cName = req.getParameter("cperson" + i);
						String pho = req.getParameter("phone" + i);
						String mail = req.getParameter("email" + i);
						try {
							SupplierUser suser = new SupplierUser(su, cName,
									pho, mail);
							setSup.add(suser);
						} catch (NullPointerException e) {
							e.printStackTrace();
						}

					}
					su.setSuppuser(setSup);
					try{
						supplierDAO.insert(su);
					}catch(Exception ex){
						ex.printStackTrace();
					}
			}
			model=new ModelAndView("/jsp/Sources.jsp");
			
			model.addObject("submenu", "admin");
			model.addObject("menuName","admin");
			model.addObject("savesrc","Successfully Inserted Source Details");
			model.setViewName("redirect:open.do");
			 return  model;
}
	/*	@RequestMapping("/editopen.do")
		public ModelAndView editopen(HttpServletRequest req,HttpSession session, ModelAndView model) throws Exception{
			Business bus=(Business)session.getAttribute("business");
			if(bus==null)return new ModelAndView("/jsp/index.jsp");
			SourcesDAO s=new SourcesDAO();
			List<Sources> ls=s.findAll(bus.getBusinessId());
			model.addObject("sources-List",ls);
			logger.info("Sources List In Controller Is:"+ls);
			model.addObject("submenu", "admin");
			model.addObject("menuName","admin");
			model.setViewName("/jsp/Sources.jsp");
			return model;
			}*/
		@RequestMapping(value="/edit.do",method = RequestMethod.GET)
			public @ResponseBody String edit(HttpServletRequest req,HttpSession session, ModelAndView model) throws Exception{
			Business bus=(Business)session.getAttribute("business");
			sourcesList=sourcesDAO.findAll(bus.getBusinessId());
			model.addObject("sources-List",sourcesList);
			logger.info("Sources List In Controller Is:"+sourcesList);
			StringBuilder sb=new StringBuilder();
		
			Iterator<Sources> itr=sourcesList.iterator();
			while(itr.hasNext()==true){
				Object ob=itr.next();
				Sources cli=(Sources)ob;
				sb.append(cli.getSourceName());
				sb.append(",");
			}
			return sb.toString();
			
			
			}

		@RequestMapping("/search.do")
		public ModelAndView search(HttpServletRequest req,HttpSession session, ModelAndView model) throws Exception{
			Business bus=(Business)session.getAttribute("business");
			if(bus==null)return new ModelAndView("/jsp/index.jsp");
			String soName=req.getParameter("soName");
			s=sourcesDAO.getByName(soName,bus.getBusinessId());
			model.addObject("sources",s);
			logger.info("Sources List In Controller Is:"+s);
			model.addObject("submenu", "admin");
			model.addObject("menuName","admin");
			model.setViewName("/jsp/editSources.jsp");
			return model;
		}
		@RequestMapping(value="/editSave.do",method = RequestMethod.POST)
		public ModelAndView update(HttpServletRequest req,HttpSession session, ModelAndView model) throws Exception{
			Business bus=(Business)session.getAttribute("business");
			if(bus==null)return new ModelAndView("/jsp/index.jsp");
			String name=req.getParameter("name");
		String email=req.getParameter("email");
		String phone=req.getParameter("phone");
		String address=req.getParameter("address");
		String website=req.getParameter("website");
		String fax=req.getParameter("fax");
		String country=req.getParameter("country");
		String cell=req.getParameter("cell");
		String assupplier=req.getParameter("assupplier");
		System.out.println("Check Box Value Is:"+assupplier);
		String sourceid=req.getParameter("sourceid");
		int id=Integer.parseInt(sourceid);
		
		
		s=new Sources(bus,name,email,phone,address,website,fax,country,cell);
		s.setId(id);
			String msg=null;
			try{
				sourcesDAO.insert(s);
				msg="Successfully Updated Source Details";
			}catch(Exception ex){
				ex.printStackTrace();
				msg="Failed To  Update Source Details.Please Try again";
			}
			
			return model=new ModelAndView("/jsp/Sources.jsp")
			.addObject("submenu", "admin")
			.addObject("menuName","admin")
			.addObject("savesrc",msg);

		}
		@RequestMapping("/delete.do")
		public ModelAndView delete(HttpServletRequest req,HttpSession session, ModelAndView model) throws Exception{
			
			Business bus=(Business)session.getAttribute("business");
			if(bus==null)return new ModelAndView("/jsp/index.jsp");
			sourcesList=sourcesDAO.findAll(bus.getBusinessId());
			model.addObject("sources-List",sourcesList);
			logger.info("Sources List In Controller Is:"+sourcesList);
			model.setViewName("/jsp/deleteSource.jsp");
			model.addObject("submenu", "admin");
			model.addObject("menuName","admin");
			return model;
		}
		@RequestMapping("/deleteSource.do")
		public ModelAndView deleteSource(HttpServletRequest req,HttpSession session, ModelAndView model) throws Exception{
			Business bus=(Business)session.getAttribute("business");
			if(bus==null)return new ModelAndView("/jsp/index.jsp");
			String source=req.getParameter("soName");
			s=sourcesDAO.getByName(source,bus.getBusinessId());
			String msg=null;
			try{
				sourcesDAO.deleteSource(s.getId(),bus.getBusinessId());
				msg="Successfully Deleted Source";
			}catch(Exception ex){
				ex.printStackTrace();
				msg="Failed To Deleted Source.Please Try Again";
			}
			return model=new ModelAndView("/jsp/Sources.jsp")
			.addObject("submenu", "admin")
			.addObject("menuName","admin")
			.addObject("msg",msg);
		}
	}
